package net.knightwatch.kwtools.walkthrough.items;

import java.util.ArrayList;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;

public class AutoCompleteAnswer extends Answer {
	private static final String TAG = "AutoCompleteAnswerType";
	// properties go here for the autocomplete type
	private ArrayList<String> recentAnswers;
	private String answer;
	private ArrayAdapter<String> adapter;
	private String hintText;
	private AutoCompleteTextView autoComplete;
	// Default Constructor
	public AutoCompleteAnswer(){
		this.setAnswerTypeId(AUTOCOMPLETE_TYPE);
		this.recentAnswers = new ArrayList<String>();
	}
	public AutoCompleteAnswer(Long questionId){
		Log.d(TAG, "Created new AutoCompleteAnswerType");
		this.setAnswerTypeId(AUTOCOMPLETE_TYPE);
		this.recentAnswers = new ArrayList<String>();
		
	}
	
	// Methods
	public ArrayList<String> getRecentAnswers(){
		return this.recentAnswers;
	}
	public void setRecentAnswers(ArrayList<String> recentAnswers){
		// Set the list of recent answers for the questionID of the question
		// convert raw data into a unique list of answers with percentages of answer frequency		
		this.recentAnswers = recentAnswers;	
	}

	@Override
	public LinearLayout getAnswerLayout(Context context) {
		// Create the layout and create in the current context
		LinearLayout linearLayout = new LinearLayout(context);
		autoComplete = new AutoCompleteTextView(context);
		autoComplete.setHint(getHintText());
		Log.v(TAG, "Creating autocomplete array adapter"); 
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_dropdown_item_1line, this.recentAnswers);
        autoComplete.setAdapter(adapter);
        Log.v(TAG, "Adding auto complete view to layout"); 
 		autoComplete.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long arg3) {
				Log.d(TAG, "Item has been selected as " + adapterView.getItemAtPosition(position));
				setAnswer(adapterView.getItemAtPosition(position).toString());
			}
			
		});
 		autoComplete.addTextChangedListener(new TextWatcher(){

			public void afterTextChanged(Editable s) {
				// Set the answer of the AutoComplete question to the currenet string value that has been typed in. 
				setAnswer(s.toString());
				
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {				
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}
 			
 		});
		linearLayout.addView(autoComplete);
		return linearLayout;
	}

	@Override
	public String toString() {
		if(autoComplete.getText().toString() != this.answer){
			setAnswer(this.autoComplete.getText().toString());
		}
		return getAnswer();
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public ArrayAdapter<String> getAdapter() {
		return adapter;
	}

	public void setAdapter(ArrayAdapter<String> adapter) {
		this.adapter = adapter;
	}
	public void setHintText(String hintText){
		this.hintText = hintText;
	}
	public String getHintText(){
		return this.hintText;
	}
	@Override
	public void updateAnswerView() {
		// TODO Update the autocomplete answer
		
	}
	@Override
	public void setAnswerText(String answerText) {
		// TODO set the answer string to the correct value
		setAnswer(answerText);
		autoComplete.setText(answerText);
	}

}
